#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define DEBUG 0
#define NOISY_TEST 1

int CheckUpdateCoins(int player, struct gameState *post, int bonus)
{
	struct gameState pre;
	memcpy (&pre, post, sizeof(struct gameState));
  int i;
  int result;
        SelectStream(2);
        PutSeed(3);
	result = updateCoins(player, post, bonus);

	
  //reset coin count
  pre.coins = 0;

  //add coins for each Treasure card in player's hand
  for (i = 0; i < pre.handCount[player]; i++)
    {
      if (pre.hand[player][i] == copper)
	{
	  pre.coins += 1;
	}
      else if (pre.hand[player][i] == silver)
	{
	  pre.coins += 2;
	}
      else if (pre.hand[player][i] == gold)
	{
	  pre.coins += 3;
	}	
    }	

  //add bonus
  pre.coins += bonus;
  assert(result==0);
  assert(memcmp(&pre, post, sizeof(struct gameState)) == 0);

  return 0;
}

int main () {

  int i, n, r, p, bonus, handPos, deckCount, discardCount, handCount;

  struct gameState G;

  printf ("Testing updateCoins.\n");

  printf ("RANDOM TESTS.\n");

  SelectStream(2);
  PutSeed(3);

          for (n = 0; n < 2000; n++) {
                for (i = 0; i < sizeof(struct gameState); i++) {
                        ((char*) &G)[i] = floor(Random() * 256);
                }
        
                int numberOfPlayers = floor(Random() * MAX_PLAYERS);
                p = floor(Random() * numberOfPlayers);
                
                G.handCount[p] = floor(Random() * MAX_HAND);
                G.deckCount[p] = floor(Random() * MAX_DECK);
                G.discardCount[p] = floor(Random() * MAX_DECK);
                G.playedCardCount = floor(Random() * MAX_DECK);
                handPos = floor(Random() * G.handCount[p]);
                G.numActions = 0;
                G.whoseTurn = p;
                G.numPlayers = numberOfPlayers;
                
                //random vals
                bonus = floor(Random() * 256);
                
                CheckUpdateCoins(p, &G, bonus);
        }

        printf("ALL TESTS OK\n");
        return 0;

  return 0;
}
